// GameWindow.cpp
#include "gamewindow.h"
#include <QPainter>
#include <QKeyEvent>

GameWindow::GameWindow(QWidget *parent) : QWidget(parent) {
    setFixedSize(800, 600);
    setFocusPolicy(Qt::StrongFocus);
    setMouseTracking(true);

    // 初始化玩家
    playerPos = QPoint(width()/2, height()/2);
    playerRadius = 20;
    score = 0;
    useMouseControl = false;

    // 生成初始食物
    for(int i=0; i<20; i++){
        generateFood();
    }

    timerId = startTimer(16); // ~60FPS
}

void GameWindow::generateFood() {
    Food food;
    food.position = QPoint(
        QRandomGenerator::global()->bounded(50, width()-50),
        QRandomGenerator::global()->bounded(50, height()-50)
    );
    food.radius = QRandomGenerator::global()->bounded(10, 30);
    food.color = QColor::fromHsv(
        QRandomGenerator::global()->bounded(360),
        255, 255
    );
    foods.append(food);
}

void GameWindow::paintEvent(QPaintEvent *) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.fillRect(rect(), QColor(30, 30, 30));

    // 绘制食物
    foreach(const Food &food, foods) {
        painter.setBrush(food.color);
        painter.drawEllipse(food.position, food.radius, food.radius);
    }

    // 绘制玩家
    painter.setBrush(QColor(0, 200, 255));
    painter.drawEllipse(playerPos, playerRadius, playerRadius);

    // 绘制分数
    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 16));
    painter.drawText(10, 30, QString("Size: %1").arg(playerRadius));
    painter.drawText(10, 60, QString("Score: %1").arg(score));
}

void GameWindow::timerEvent(QTimerEvent *event) {
    if (event->timerId() == timerId) {
        // 自动向鼠标位置移动
        if(useMouseControl) {
            moveTowards(mapFromGlobal(QCursor::pos()));
        }
        
        checkCollisions();
        update();
    }
}

void GameWindow::checkCollisions() {
    for(int i=0; i<foods.size(); i++){
        const Food &food = foods[i];
        QPoint delta = food.position - playerPos;
        float distance = sqrt(delta.x()*delta.x() + delta.y()*delta.y());
        
        // 当玩家半径大于食物时才能吞噬
        if(distance < playerRadius - food.radius/2) {
            playerRadius += food.radius * 0.2;  // 按比例增长
            score += food.radius;
            foods.removeAt(i);
            generateFood(); // 生成新食物
            i--;
        }
    }
}

// 移动控制（键盘方向键或鼠标）
void GameWindow::keyPressEvent(QKeyEvent *event) {
    int speed = 5 + playerRadius/10;
    switch(event->key()) {
    case Qt::Key_Left:  playerPos.rx() -= speed; break;
    case Qt::Key_Right: playerPos.rx() += speed; break;
    case Qt::Key_Up:    playerPos.ry() -= speed; break;
    case Qt::Key_Down:  playerPos.ry() += speed; break;
    case Qt::Key_M:     useMouseControl = !useMouseControl; break;
    }
}

void GameWindow::mouseMoveEvent(QMouseEvent *event) {
    if(useMouseControl) {
        moveTowards(event->pos());
    }
}

void GameWindow::moveTowards(const QPoint &target) {
    int speed = 3 + playerRadius/15;
    QPoint direction = target - playerPos;
    float length = sqrt(direction.x()*direction.x() + direction.y()*direction.y());
    
    if(length > 0) {
        direction = direction * speed / length;
        playerPos += direction;
        
        // 边界限制
        playerPos.setX(qBound(playerRadius, playerPos.x(), width()-playerRadius));
        playerPos.setY(qBound(playerRadius, playerPos.y(), height()-playerRadius));
    }
}